#!/bin/sh
# author:gaoxu
# description:tiktokdownloader start

readonly download_tiktok_path="/Users/gaoxu/personal_workspace/TikTokDownloader/Download/"
readonly target_tiktok_path="/Users/gaoxu/Downloads/copy/"
readonly download_xima_path="/Users/gaoxu/personal_workspace/Ximalaya-Downloader/Download/"
readonly target_xima_path="/Users/gaoxu/Downloads/yinpin/"

continue_loop_down="yes"

while [ "$continue_loop_down" = "yes" ]; do
    # 选择操作模式
    read -p '请选择下载渠道: 1-抖音, 2-喜马拉雅, 3-小红书: ' mode
    if [ "$mode" = "1" ]; then
        echo '启动抖音下载器'
        cd /Users/gaoxu/personal_workspace/TikTokDownloader
        echo '启动python虚拟环境'
        source ./venv/bin/activate
        echo '启动python虚拟环境成功'
        python main.py
        if [ -d "$download_tiktok_path" ];then
            if [ "$(ls -A $download_tiktok_path)" ]; then
                mv "$download_tiktok_path"* "$target_tiktok_path"
            else
                echo '暂时没有需要移动的文件!'
            fi
        else
            echo '路径不存在,请检查代码!'
        fi
        echo '抖音下载器退出成功!'
    elif [ "$mode" = "2" ]; then
        echo '启动喜马拉雅下载器'
        cd /Users/gaoxu/personal_workspace/Ximalaya-Downloader
        echo '启动python虚拟环境'
        source ./venv/bin/activate
        echo '启动python虚拟环境成功'
        python cli.py
        echo '开始移动下载文件'
        if [ -d "$download_xima_path" ];then
            if [ "$(ls -A $download_xima_path)" ]; then
                 mv "$download_xima_path"* "$target_xima_path"
            else
                echo '暂时没有需要移动的文件!'
            fi
        else
             echo '路径不存在,请检查代码!'
        fi
        echo '喜马拉雅下载器退出成功!'
    elif [ "$mode" = "3" ]; then
        echo '启动小红书下载器'
        cd /Users/gaoxu/personal_workspace/XHS-Downloader
        echo '启动python虚拟环境'
        source ./venv/bin/activate
        echo '启动python虚拟环境成功'
        continue_loop="yes"
        xhs_prefix_url="https://www.xiaohongshu.com/explore"
        while [ "$continue_loop" = "yes" ]; do
            # 下载视频到笔记
            read -p "请输入你要下载笔记id:" id
            
            python main.py -u "$xhs_prefix_url/$id" -us -rd true
            # 在每次循环迭代时，询问用户是否要继续
            echo "Do you want to continue? (yes/no)"
            read continue_loop
        done
        echo '小红书下载器退出成功'
    else
        echo "参数有误!,无此模式!"
    fi
    echo "Do you want to continue download? (yes/no)"
    read continue_loop_down
done